<html>
<head>
<title>TestHanoi.java</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<style type="text/css">
.s0 { color: #808080; font-style: italic;}
.s1 { color: #000000;}
.s2 { color: #000080; font-weight: bold;}
.s3 { color: #008000; font-weight: bold;}
.s4 { color: #0000ff;}
.ls0 { height: 1px; border-width: 0; color: #c0c0c0; background-color:#c0c0c0}
</style>
</head>
<body bgcolor="#ffffff">
<table CELLSPACING=0 CELLPADDING=5 COLS=1 WIDTH="100%" BGCOLOR="#606060" >
<tr><td><center>
<font face="Arial, Helvetica" color="#000000">
TestHanoi.java</font>
</center></td></tr></table>
<pre><span class="s0">/**</span>
 <span class="s0">* Created with IntelliJ IDEA.</span>
 <span class="s0">* Description:</span>
 <span class="s0">* User:</span>
 <span class="s0">* Date: 2024-12-12</span>
 <span class="s0">* Time: 19:37</span>
 <span class="s0">*/</span>
<span class="s2">import </span>java.util.Scanner;
<span class="s2">import </span>java.util.InputMismatchException;

<span class="s2">public class </span>TestHanoi {
    <span class="s2">public static void </span>main(String[] args) {
        System.out.println(<span class="s3">&quot;一共有多少个盘子：&quot;</span>);
        Scanner sc = <span class="s2">new </span>Scanner(System.in);
        <span class="s2">try </span>{
            <span class="s2">int </span>n = sc.nextInt();
            <span class="s2">if </span>(n &lt;= <span class="s4">0</span>) {
                <span class="s2">throw new </span>IllegalArgumentException(<span class="s3">&quot;圆盘数量必须是正整数。&quot;</span>);
            }
            hanoi(n, <span class="s3">'A'</span>, <span class="s3">'B'</span>, <span class="s3">'C'</span>);
        } <span class="s2">catch </span>(InputMismatchException e) {
            System.out.println(<span class="s3">&quot;输入错误：请输入一个有效的整数。&quot;</span>);
        } <span class="s2">catch </span>(IllegalArgumentException e) {
            System.out.println(e.getMessage());
        } <span class="s2">finally </span>{
            sc.close();
        }
    }
<hr class="ls0">    <span class="s2">public static void </span>hanoi(<span class="s2">int </span>n, <span class="s2">char </span>begin, <span class="s2">char </span>mid, <span class="s2">char </span>end) {
        <span class="s0">//只有一个盘子</span>
        <span class="s2">if </span>(n == <span class="s4">1</span>) {
            System.out.println(<span class="s3">&quot;把第1个盘子从&quot; </span>+ begin + <span class="s3">&quot;移到&quot; </span>+ end);
            <span class="s0">//无论有多少个盘子，都认为只有两个，上面的所有盘子和最下面的一个盘子</span>
        } <span class="s2">else </span>{
            <span class="s0">//移动上面所有的盘子到中间位置</span>
            hanoi(n - <span class="s4">1</span>, begin, end, mid);
            <span class="s0">//移动下面的盘子</span>
            System.out.println(<span class="s3">&quot;把第&quot; </span>+ n + <span class="s3">&quot;个盘子从&quot; </span>+ begin + <span class="s3">&quot;移到&quot; </span>+ end);
            <span class="s0">//把上面的所有盘子从中间位置移到目标位置</span>
            hanoi(n - <span class="s4">1</span>, mid, begin, end);
        }

    }
}</pre>
</body>
</html>